//
//  MOTextView.m
//  MOTextField
//
//  Created by  on 16/3/20.
//  Copyright © 2016年 . All rights reserved.
//

#import "MOTextView.h"
#import "UIView+Frame.h"
#import "MOTool.h"

@interface MOTextView()
/**
 *  提醒文字
 */
@property (nonatomic,weak) UILabel *label_placeholder;
/**
 *  最大值提醒
 */
@property (nonatomic,weak) UILabel *label_maxWarning;

@end

@implementation MOTextView

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self == [super initWithCoder:aDecoder]) {
        [self initializer];
    }
    
    
    
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame]) {
        [self initializer];
    }
    return self;
}
/**
 *  初始化
 */
- (void)initializer
{
    //注册输入通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
}
- (void)textViewDidChange:(NSNotification *)noti
{
    MOTextView *mo_textView = noti.object;
    if (mo_textView == self) {
        [self updatePlaceholder];
        [self updateWarning];
        /**
         *  禁止输入内容超出最大限制
         */
        if(self.text.length > self.max_char-1) {
            NSRange range = {0,self.max_char-1}; //  从哪个位置开始截取，截取多长
            self.text = [self.text substringWithRange:range];
        }
        /**
         *  block返回输入内容
         */
        if (self.didChangeTextView != nil) {
            self.didChangeTextView(mo_textView);
        }
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    [self updatePlaceholder];
    [self updatePlaceholder];
}
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    [self updatePlaceholder];
}
- (void)updatePlaceholder
{
    if (self.text.length == 0) {
        self.label_placeholder.font = self.font;
        self.label_placeholder.numberOfLines = 0;
        self.label_placeholder.text = self.placeholder;
        self.label_placeholder.textColor = [UIColor lightGrayColor];
        [self.label_placeholder sizeToFit];
        CGFloat label_higth = [MOTool MOtextSizeH:self.placeholder WithWidth:self.width WithFount:self.font];
        self.label_placeholder.frame = CGRectMake(4, 5, self.width-8, label_higth+4);
        
    }
    else{
        [self.label_placeholder removeFromSuperview];
    }
}
- (void)updateWarning
{
    if(self.max_char > 0){
        self.label_maxWarning.font = self.font;
        self.label_maxWarning.numberOfLines = 0;
        self.label_maxWarning.textAlignment = UITextAlignmentRight;
        self.label_maxWarning.textColor = [UIColor lightGrayColor];
        [self.label_maxWarning sizeToFit];
        NSString *str = [NSString stringWithFormat:@"%lu/%d",(unsigned long)self.text.length,self.max_char];
        self.label_maxWarning.text = str;
        self.label_maxWarning.frame = CGRectMake(4,self.height - [UIFont systemFontSize], self.width-8,[UIFont systemFontSize]);
    }
}
- (UILabel *)label_placeholder
{
    if (_label_placeholder == nil) {
        UILabel *label_placeholder = [[UILabel alloc]init];
        _label_placeholder = label_placeholder;
        [self addSubview:_label_placeholder];
    }
    return _label_placeholder;
}

- (UILabel *)label_maxWarning
{
    if (_label_maxWarning == nil) {
        UILabel *label_maxWarning = [[UILabel alloc]init];
        _label_maxWarning = label_maxWarning;
        [self addSubview:_label_maxWarning];
    }
    return _label_maxWarning;
}

@end
